Skip to content

Cleanup Zeroable and ZeroableOptions.#118

Open
mqqz wants to merge 3 commits intoRust-for-Linux:mainfrom
mqqz:cleanup-zeroable
Open

Cleanup Zeroable and ZeroableOptions.#118
mqqz wants to merge 3 commits intoRust-for-Linux:mainfrom
mqqz:cleanup-zeroable

Conversation

@mqqz
Copy link

@mqqz mqqz commented Mar 10, 2026

Did my part of janitor work to help cleanup Zeroable and ZeroableOption as suggested in #116:

  • Ensure consistency in placement: put the Zeroable trait and its implementations (including macro invocations) first in the relevant section of src/lib.rs, followed by the ZeroableOption trait and its implementations.
  • One of the macros is currently named impl_non_zero_int_zeroable_option; it should be renamed to impl_zeroable_option for consistency.
  • Use the impl_zeroable_option macro to handle generic impls for types like &T, &mut T, NonNull<T>, and others (for which Option<T> is guaranteed to be zeroable).

Added an extra commit for minimal testing which I'm not sure how helpful it is but feel free to not include:

  • tests/zeroing.rs coverage that zeroed::<Option<T>>() is None for refs, NonNull, and nonzero ints.
  • generic compile-check coverage for T: ZeroableOption (&u8, &mut u8, NonNull<u8>, NonZeroU8).
  • UI compile-fail test zeroable/option_string_not_zeroable.rs to verify Option<String> is not Zeroable.

Closes: #116

mqqz added 3 commits March 10, 2026 09:56
Place definitions and implementations (incl. macro invocations) of
the `Zeroable` trait first in the relevant section of `src/lib.rs`,
followed by the ZeroableOption trait and its implementations.

Rename `impl_non_zero_int_zeroable_option` to `impl_zeroable_option`
for consistency.

This commit should not introduce any functional changes.

Signed-off-by: Mohamad Alsadhan <mo@sdhn.cc>
Improve impl_zeroable_option macro to handle generic impls for types
like `&T`, `&mut T`, `NonNull<T>`, and others (for which `Option<T>`
is guaranteed to be zeroable) with similar approach to
`impl_zeroable`.

Signed-off-by: Mohamad Alsadhan <mo@sdhn.cc>
Minimal testing including:
- add `tests/zeroing.rs` coverage that `zeroed::<Option<T>>()` is
  `None` for refs, `NonNull`, and nonzero ints.
- add generic compile-check coverage for `T: ZeroableOption` (`&u8`,
  `&mut u8`, `NonNull<u8>`, `NonZeroU8`).
- add UI compile-fail test `zeroable/option_string_not_zeroable.rs`
  to verify `Option<String>` is not `Zeroable`.

Not entirely sure how useful these tests are but it's on a different
commit so feel free to not include them.

Signed-off-by: Mohamad Alsadhan <mo@sdhn.cc>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Cleanup macros around Zeroable & ZeroableOption

1 participant